package com.sale.salesystem.controller;

import com.sale.salesystem.service.DashboardPermissionService;
import com.sale.salesystem.vo.DashboardPermissionVO;
import com.sale.salesystem.vo.ResultVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仪表板权限管理接口
 */
@RestController
@RequestMapping("/api/dashboard-permission")
@RequiredArgsConstructor
@CrossOrigin
@Slf4j
public class DashboardPermissionController {

    private final DashboardPermissionService permissionService;

    /**
     * 获取用户可访问的仪表板列表
     */
    @GetMapping("/list")
    public ResultVO<List<DashboardPermissionVO>> getUserDashboards(@RequestParam Long userId) {
        return ResultVO.success(permissionService.getUserDashboards(userId));
    }

    /**
     * 获取仪表板信息
     *
     * @param id 仪表板ID
     * @return 仪表板信息，包括所有者和访问者
     * <p>
     * 示例请求：
     * GET /api/dashboard-permission/1
     * <p>
     * 示例响应：
     * {
     * "code": 200,
     * "message": "success",
     * "data": {
     * "dashboardId": 1,
     * "dashboardName": "核心数据分析",
     * "dashboardType": "core",
     * "ownerName": "admin",
     * "visitorNames": ["zhangsan", "lisi"]
     * }
     * }
     */
    @GetMapping("/{id}")
    public ResultVO<DashboardPermissionVO> getDashboardInfo(@PathVariable Long id) {
        return ResultVO.success(permissionService.getDashboardInfo(id));
    }

    /**
     * 获取仪表板访问者列表
     *
     * @param id 仪表板ID
     * @return 仪表板访问者信息
     * <p>
     * 示例请求：
     * GET /api/dashboard-permission/1/visitors
     * <p>
     * 示例响应：
     * {
     * "code": 200,
     * "message": "success",
     * "data": {
     * "dashboardId": 1,
     * "visitorNames": ["zhangsan", "lisi"]
     * }
     * }
     */
    @GetMapping("/{id}/visitors")
    public ResultVO<DashboardPermissionVO> getVisitors(@PathVariable Long id) {
        return ResultVO.success(permissionService.getVisitors(id));
    }

    /**
     * 添加仪表板访问者
     *
     * @param dashboardId       仪表板ID
     * @param username 访问者用户名
     *                 <p>
     *                 示例请求：
     *                 POST /api/dashboard-permission/1/visitor
     *                 Body: "zhangsan"
     *                 <p>
     *                 示例响应：
     *                 {
     *                 "code": 200,
     *                 "message": "success"
     *                 }
     */
    @GetMapping("/{id}/visitor")
    public ResultVO<Void> addVisitor(
        @PathVariable("id") Long dashboardId,
        @RequestParam("username") String username
    ) {
        permissionService.addVisitor(dashboardId, username);
        return ResultVO.success(null);
    }

    /**
     * 移除仪表板访问者
     *
     * @param id       仪表板ID
     * @param username 访问者用户名
     *                 <p>
     *                 示例请求：
     *                 DELETE /api/dashboard-permission/1/visitor/zhangsan
     *                 <p>
     *                 示例响应：
     *                 {
     *                 "code": 200,
     *                 "message": "success"
     *                 }
     */
    @DeleteMapping("/{id}/visitor/{username}")
    public ResultVO<Void> removeVisitor(@PathVariable Long id, @PathVariable String username) {
        permissionService.removeVisitor(id, username);
        return ResultVO.success(null);
    }

    /**
     * 转让仪表板所有权
     *
     * @param id       仪表板ID
     * @param username 新所有者用户名
     *                 <p>
     *                 示例请求：
     *                 PUT /api/dashboard-permission/1/owner
     *                 Body: "zhangsan"
     *                 <p>
     *                 示例响应：
     *                 {
     *                 "code": 200,
     *                 "message": "success"
     *                 }
     */
    @PutMapping("/{id}/owner")
    public ResultVO<Void> transferOwner(@PathVariable Long id, @RequestBody String username) {
        permissionService.transferOwner(id, username);
        return ResultVO.success(null);
    }

    /**
     * 检查用户是否有权限访问仪表板
     */
    @GetMapping("/{id}/check")
    public ResultVO<Boolean> checkPermission(
            @PathVariable Long id,
            @RequestParam Long userId
    ) {
        return ResultVO.success(permissionService.hasPermission(userId, id));
    }
}